package com.esri.core.internal.catalog;

import com.esri.core.internal.catalog.Group;
import com.esri.core.internal.catalog.Item;
import com.esri.core.internal.catalog.User;
import com.esri.core.internal.io.handler.h;
import com.esri.core.internal.io.handler.n;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.CallbackListener;
import com.esri.core.portal.WebMap;
import java.io.InterruptedIOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.achartengine.internal.a;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class Catalog {
    static final /* synthetic */ boolean a = true;
    private static long j = 3600;
    private static long k = 31536000;
    private URL b;
    private URL c = null;
    private String d = null;
    private String e = null;
    private String f = null;
    private Date g = null;
    private User h;
    private n i;

    public Catalog(URL url) {
        this.b = null;
        this.b = url;
    }

    public void setDefaultTokenExpirationTime(long j2) throws CatalogException {
        if (j2 > k) {
            throw new CatalogException("Token expiration cannot exceed a maximum of 1 year");
        }
        j = j2;
    }

    public void signInToMCS(String str, String str2) throws EsriSecurityException {
        this.i = a(this.b.toString().toLowerCase(), str, str2);
    }

    public void signIn(String str, String str2) throws CatalogException {
        HashMap hashMap = new HashMap();
        hashMap.put("username", str);
        hashMap.put("password", str2);
        hashMap.put("referer", this.b.toString());
        hashMap.put("expiration", j + "");
        try {
            JsonParser a2 = h.a(this.c + "/sharing/generateToken", hashMap);
            while (a2.nextToken() != JsonToken.END_OBJECT) {
                String currentName = a2.getCurrentName();
                a2.nextToken();
                if (currentName.equals("token")) {
                    this.f = a2.getText();
                } else if (currentName.equals("expires")) {
                    this.g = new Date(a2.getLongValue());
                }
            }
            this.d = str;
            this.e = str2;
        } catch (Exception e) {
            throw new CatalogException("Sign in failed", e.getCause());
        }
    }

    public List<Folder> getMyContents() throws CatalogException {
        if (getUserToken() == null) {
            throw new RuntimeException("Access denied. User content is accessible only to signed in users");
        }
        HashMap hashMap = new HashMap();
        hashMap.put("token", getUserToken());
        ArrayList arrayList = new ArrayList();
        Folder folder = new Folder(this, true);
        try {
            JsonParser a2 = h.a(this.b + "/sharing/content/users/" + this.d, hashMap);
            while (a2.nextToken() != JsonToken.END_OBJECT) {
                String currentName = a2.getCurrentName();
                a2.nextToken();
                if (currentName.equals("currentFolder")) {
                    a2.getText();
                    folder.setFolderName("root");
                } else if (currentName.equals("username")) {
                    folder.setUsername(a2.getText());
                } else if (currentName.equals("items")) {
                    if (a2.getCurrentToken() != JsonToken.START_ARRAY) {
                        throw new RuntimeException("Invalid Json response..");
                    }
                    ArrayList arrayList2 = new ArrayList();
                    while (a2.nextToken() != JsonToken.END_ARRAY) {
                        Item item = new Item(this.b);
                        item.fromJson(a2, null, this);
                        arrayList2.add(item);
                    }
                    folder.setItems(arrayList2);
                } else if (!currentName.equals("folders")) {
                    continue;
                } else if (a2.getCurrentToken() != JsonToken.START_ARRAY) {
                    throw new RuntimeException("Invalid Json response..");
                } else {
                    while (a2.nextToken() != JsonToken.END_ARRAY) {
                        Folder folder2 = new Folder(this, false);
                        while (a2.nextToken() != JsonToken.END_OBJECT) {
                            String currentName2 = a2.getCurrentName();
                            a2.nextToken();
                            if (currentName2.equals("username")) {
                                folder2.setUsername(a2.getText());
                            } else if (currentName2.equals("id")) {
                                folder2.setId(a2.getText());
                            } else if (currentName2.equals(a.b)) {
                                folder2.setFolderName(a2.getText());
                            } else if (currentName2.equals("created")) {
                                folder2.setCreatedDate(new Date(a2.getLongValue()));
                            }
                        }
                        arrayList.add(folder2);
                    }
                }
            }
            arrayList.add(0, folder);
            return arrayList;
        } catch (Exception e) {
            throw new CatalogException("Unable to retrieve user's content", e.getCause());
        }
    }

    public List<Group> getMyGroups() throws CatalogException {
        if (getUserToken() == null) {
            throw new CatalogException("Access denied. User groups is accessible only to signed in users");
        }
        return getSignedInUser().getGroups();
    }

    public void signOut() {
        this.f = null;
        this.g = null;
        n nVar = this.i;
        if (nVar != null) {
            try {
                n.a(nVar.a);
            } catch (EsriSecurityException unused) {
            }
            this.i = null;
        }
    }

    public User getSignedInUser() throws CatalogException {
        if (getUserToken() == null) {
            throw new CatalogException("There is no signed in user at this time");
        }
        if (this.h == null) {
            HashMap hashMap = new HashMap();
            hashMap.put("token", getUserToken());
            try {
                JsonParser a2 = h.a(this.b + "/sharing/community/users/" + this.d, hashMap);
                User user = new User();
                this.h = user;
                user.fromJson(a2, null, this);
            } catch (Exception e) {
                throw new CatalogException("Unable to get User info for the signed in user", e.getCause());
            }
        }
        return this.h;
    }

    public ResultCursor<Item> searchFeaturedMaps(Item.ItemType itemType) throws CatalogException {
        String str = this.b + "/sharing/community/groups";
        try {
            HashMap hashMap = new HashMap();
            hashMap.put("q", "\"Esri Featured Content\" AND owner:esri");
            ResultCursor resultCursor = new ResultCursor(this, str, hashMap, h.a(str, hashMap), Group.class, null);
            if (!a && resultCursor.getCount() != 1) {
                throw new AssertionError();
            }
            String str2 = this.b + "/sharing/content/items/" + ((Group) resultCursor.nextElement()).getFeaturedItemId() + "/relatedItems";
            hashMap.clear();
            hashMap.put("q", "type:\"Web Map\"");
            hashMap.put("relationshipType", "FeaturedItems2Item");
            hashMap.put("direction", "forward");
            if (getUserToken() != null) {
                hashMap.put("token", getUserToken());
            }
            return new ResultCursor<>(this, str2, hashMap, h.a(str2, hashMap), Item.class, new Filter(itemType));
        } catch (Exception e) {
            throw new CatalogException("Search for Featured Maps failed.", e.getCause());
        }
    }

    public List<Folder> getWebMapsFromContentServer() throws CatalogException {
        ArrayList arrayList = new ArrayList();
        try {
            String url = this.b.toString();
            if (!url.endsWith("/")) {
                url = url + "/";
            }
            String str = url + "content/allItems";
            HashMap hashMap = new HashMap();
            hashMap.put("type", "Web Map");
            JsonParser a2 = h.a(str, hashMap, this.i);
            while (a2.nextToken() != JsonToken.END_OBJECT) {
                String currentName = a2.getCurrentName();
                a2.nextToken();
                if (currentName.equals("itemFolders") && a2.getCurrentToken() == JsonToken.START_ARRAY) {
                    while (a2.nextToken() != JsonToken.END_ARRAY) {
                        if (a2.getCurrentToken() == JsonToken.START_OBJECT) {
                            Folder folder = new Folder(this, true);
                            while (a2.nextToken() != JsonToken.END_OBJECT) {
                                String currentName2 = a2.getCurrentName();
                                a2.nextToken();
                                if (currentName2.equals("currentFolder")) {
                                    if (a2.getCurrentToken() == JsonToken.START_OBJECT) {
                                        while (a2.nextToken() != JsonToken.END_OBJECT) {
                                            String currentName3 = a2.getCurrentName();
                                            a2.nextToken();
                                            if (currentName3.equals("username")) {
                                                folder.setUsername(a2.getText());
                                            } else if (currentName3.equals(a.b)) {
                                                folder.setFolderName(a2.getText());
                                            } else if (currentName3.equals("id")) {
                                                folder.setId(a2.getText());
                                            }
                                        }
                                    }
                                } else if (currentName2.equals("items") && a2.getCurrentToken() == JsonToken.START_ARRAY) {
                                    ArrayList arrayList2 = new ArrayList();
                                    while (a2.nextToken() != JsonToken.END_ARRAY) {
                                        Item item = new Item();
                                        item.fromJson(a2, null, this);
                                        arrayList2.add(item);
                                    }
                                    folder.setItems(arrayList2);
                                }
                            }
                            arrayList.add(folder);
                        }
                    }
                }
            }
            return arrayList;
        } catch (Exception e) {
            throw new CatalogException("Unable to fetch WebMaps from the content server ", e);
        }
    }

    public ResultCursor<Item> searchItems(String str, Item.ItemType itemType, Item.SortField sortField, SortOrder sortOrder) throws CatalogException {
        String str2;
        try {
            String str3 = this.b + "/sharing/search";
            HashMap hashMap = new HashMap();
            if (str == null || str.equals("")) {
                str2 = "";
            } else {
                str2 = "" + str;
            }
            if (itemType != null) {
                if (!str2.equals("")) {
                    str2 = str2.concat(" AND ");
                }
                if (itemType == Item.ItemType.WEB_MAP) {
                    str2 = str2 + "type:\"Web Map\" NOT \"Web Mapping Application\"";
                } else if (itemType == Item.ItemType.FEATURE_COLLECTION) {
                    str2 = str2 + "type:\"Feature Collection\"";
                }
            }
            hashMap.put("q", str2);
            if (sortField != null) {
                hashMap.put("sortField", sortField.toString());
            }
            if (sortOrder != null) {
                hashMap.put("sortOrder", sortOrder.toString());
            }
            if (getUserToken() != null) {
                hashMap.put("token", getUserToken());
            }
            return new ResultCursor<>(this, str3, hashMap, h.a(str3, hashMap), Item.class, null);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CatalogException("Search for items failed", e.getCause());
        }
    }

    public ResultCursor<Group> searchGroups(String str, Group.SortField sortField, SortOrder sortOrder) throws CatalogException {
        try {
            String str2 = this.b + "/sharing/community/groups";
            HashMap hashMap = new HashMap();
            if (str != null && !str.equals("")) {
                hashMap.put("q", str);
            }
            if (sortField != null) {
                hashMap.put("sortField", sortField.toString());
            }
            if (sortOrder != null) {
                hashMap.put("sortOrder", sortOrder.toString());
            }
            if (getUserToken() != null) {
                hashMap.put("token", getUserToken());
            }
            return new ResultCursor<>(this, str2, hashMap, h.a(str2, hashMap), Group.class, null);
        } catch (Exception e) {
            throw new CatalogException("Search for groups failed", e.getCause());
        }
    }

    public ResultCursor<User> searchUsers(String str, User.SortField sortField, SortOrder sortOrder) throws CatalogException {
        try {
            String str2 = this.b + "/sharing/community/users";
            HashMap hashMap = new HashMap();
            if (str != null && !str.equals("")) {
                hashMap.put("q", str);
            }
            if (sortField != null) {
                hashMap.put("sortField", sortField.toString());
            }
            if (sortOrder != null) {
                hashMap.put("sortOrder", sortOrder.toString());
            }
            if (getUserToken() != null) {
                hashMap.put("token", getUserToken());
            }
            return new ResultCursor<>(this, str2, hashMap, h.a(str2, hashMap), User.class, null);
        } catch (Exception e) {
            throw new CatalogException("Search for users failed", e.getCause());
        }
    }

    public String getUserToken() throws CatalogException {
        if (this.f != null) {
            if (new Date().before(this.g)) {
                return this.f;
            }
            signIn(this.d, this.e);
            return this.f;
        }
        return null;
    }

    public URL getUrl() {
        return this.b;
    }

    public static void getItem(final String str, final String str2, final String str3, final String str4, final CallbackListener<Item> callbackListener) {
        new Runnable() { // from class: com.esri.core.internal.catalog.Catalog.1
            @Override // java.lang.Runnable
            public void run() {
                try {
                    n a2 = Catalog.a(str, str3, str4);
                    JsonParser a3 = h.a(str + "/content/items/" + str2, (Map<String, String>) null, a2);
                    Item item = new Item();
                    item.fromJson(a3, null, null);
                    item.setMCSAddress(str);
                    callbackListener.onCallback(item);
                } catch (InterruptedIOException | InterruptedException | ExecutionException unused) {
                } catch (Throwable th) {
                    callbackListener.onError(th);
                }
            }
        }.run();
    }

    public static void getWebMap(final String str, final String str2, final String str3, final String str4, final CallbackListener<WebMap> callbackListener) {
        new Runnable() { // from class: com.esri.core.internal.catalog.Catalog.2
            @Override // java.lang.Runnable
            public void run() {
                try {
                    Catalog.getItem(str, str2, str3, str4, new CallbackListener<Item>() { // from class: com.esri.core.internal.catalog.Catalog.2.1
                        {
                            AnonymousClass2.this = this;
                        }

                        @Override // com.esri.core.map.CallbackListener
                        public void onCallback(Item item) {
                            Catalog.getWebMap(item, str3, str4, callbackListener);
                        }

                        @Override // com.esri.core.map.CallbackListener
                        public void onError(Throwable th) {
                            callbackListener.onError(th);
                        }
                    });
                } catch (Throwable th) {
                    callbackListener.onError(th);
                }
            }
        }.run();
    }

    public static void getWebMap(final Item item, final String str, final String str2, final CallbackListener<WebMap> callbackListener) {
        if (item == null) {
            callbackListener.onError(new Exception("Item cannot be null."));
        } else {
            new Runnable() { // from class: com.esri.core.internal.catalog.Catalog.3
                @Override // java.lang.Runnable
                public void run() {
                    try {
                        n a2 = Catalog.a(item.getMCSAddress(), str, str2);
                        callbackListener.onCallback(WebMap.newInstance(h.a(item.getMCSAddress() + "/content/items/" + item.getId() + "/data", (Map<String, String>) null, a2), item.getExtent(), item.getSpRef()));
                    } catch (InterruptedIOException | InterruptedException | ExecutionException unused) {
                    } catch (Throwable th) {
                        callbackListener.onError(th);
                    }
                }
            }.run();
        }
    }

    static n a(String str, String str2, String str3) throws EsriSecurityException {
        if (str == null) {
            return null;
        }
        String lowerCase = str.toLowerCase();
        if (lowerCase.endsWith("/")) {
            lowerCase = lowerCase.substring(0, lowerCase.length() - 1);
        }
        if (lowerCase.contains("/")) {
            lowerCase = lowerCase.substring(0, lowerCase.lastIndexOf("/"));
        }
        String str4 = lowerCase + "/rest/";
        UserCredentials userCredentials = new UserCredentials();
        if (str2 != null && str2.trim().length() != 0 && str3 != null && str3.trim().length() != 0) {
            userCredentials.setUserAccount(str2, str3);
        }
        userCredentials.setAuthenticationType(UserCredentials.AuthenticationType.HTTP);
        return n.a(str4, userCredentials);
    }

    public n a() {
        return this.i;
    }
}
